<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		
		<view class="sugg-list">
			<view class="sugg-item" v-for="(item,i) in searchResults[0]" :key="i" @click="gotoDetail(item)">
				<view class="goods-name">{{item}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//延时器的timerId
				timer:null,
				//搜索关键词
				kw:'',
				searchResults:[]
			};
		},
		methods:{
			input(e){
				//清除timer对应的延时器
				clearTimeout(this.timer)
				//重新启动一个延时器
				this.timer=setTimeout(() =>{
					//如果500毫秒内，没有触发新的输入事件，则为搜索关键词赋值
				this.kw=e
				this.getSearchList()
					
					
				},500)
			},
			async getSearchList(){
				if(this.kw === ''){
					this.searchResults=[]
					return
				}
				const { data:res }=await uni.$http.post('http://127.0.0.1:5000/search',{ data_title:this.kw })
				if(res.meta.status !== 200)return uni.$showMsg()
				this.searchResults=res.message[0].health_title
				console.log(this.searchResults)
			
			},
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?data_title=' + item
				})
			}
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
}
.sugg-list{
	padding: 0 5px;
	
	.sugg-item{
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-between;
		
		.goods-name{
			//文字不允许换行
			white-space: nowrap;
			//溢出隐藏
			overflow: hidden;
			//文本溢出后，使用。。。代替
			text-overflow: ellipsis;
			margin-right: 3px;
		}
	}
}
</style>
